package view;

import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.DebateFormat;
import model.MainModel;


class DebateCountSpinner extends JSpinner implements ChangeListener {

	private DebateFormat format;
	private MainModel model;

	public DebateCountSpinner(DebateFormat format, MainModel model) {
		super(new SpinnerNumberModel(model.getDebateCount(format), 0,
				Integer.MAX_VALUE, 1));
		this.format = format;
		this.model = model;
		model.addChangeListener(this);
		this.addChangeListener(this);
	}

	@Override
	public void stateChanged(ChangeEvent evt) {
		if (evt.getSource() == this) {
			model.setDebateCount(format, (Integer) getValue());
		} else {
			// model change occured
			setValue(model.getDebateCount(format));
		}
	}

}
